import 'dart:math';

import 'package:flutter/material.dart';

class RoomList extends StatefulWidget {
  RoomList({super.key, required this.point, required this.building});
//小区信息
  String point;
  // 楼栋信息
  String building;

  @override
  State<RoomList> createState() => _RoomListState();
}

class _RoomListState extends State<RoomList> {
  // 1. 定义初始数据
  Map data = {
    'point': '', // 小区信息
    'building': '', // 楼栋信息
    'rooms': ['101', '102'], // 随机产生的房间号
  };

  @override
  void initState() {
    super.initState();

    // 伪造数据
    fakeRoom();
  }

// 伪造数据
  fakeRoom() {
    // 随机产生房间的数量 5-8
    int size = Random().nextInt(4) + 5;
    //  遍历房间个数随机产生房间号  102 2002
    List rooms = [];
    for (int i = 0; i < size; i++) {
      // 随机产生房间号  房间号的组成 ：楼层号0房间号
      // 1.2.1 随机产生楼层 1-20
      int floor = Random().nextInt(20) + 1; // 1-20
      // 1.2.2 随机产生房间号 1-4
      int no = Random().nextInt(4) + 1;
      // 1.2.3 组合房间号放入数组
      String room = '${floor}0$no';
      if (rooms.contains(room)) continue; // 去重处理
      rooms.add(room);
    }

    setState(() {
      //  获取数据
      data['point'] = widget.point;
      data['building'] = widget.building;
      data['rooms'] = rooms;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: const Text('选择房间'),
        ),
        body: ListView.builder(
          itemBuilder: (BuildContext context, int index) {
            return Container(
                color: Colors.white,
                padding: const EdgeInsets.all(10),
                child: GestureDetector(
                    onTap: () {
                      Navigator.pushNamed(context, '/house_form', arguments: {
                        'point': data['point'],
                        'building': data['building'],
                        'room': data['rooms'][index]
                      });
                    },
                    child: Row(children: [
                      Expanded(
                          child: Text(
                              '${data['point']}${data['building']}${data['rooms'][index]}室')),
                      const Row(children: [
                        Icon(Icons.arrow_forward_ios,
                            size: 16, color: Colors.black),
                      ])
                    ])));
          },
          itemCount: (data['rooms'] as List).length,
        ));
  }
}
